unit unQueryBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BusinessLogicInterface, QueryModuleInterface, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxContainer, cxEdit, dxSkinsCore,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinsDefaultPainters,
  DBGridEhGrouping, GridsEh, DBGridEh, cxGroupBox, dxSkinsdxBarPainter, dxBar,
  cxClasses, dxSkinBlack, dxSkinBlue, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkRoom, dxSkinDarkSide, dxSkinFoggy, dxSkinGlassOceans,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinPumpkin, dxSkinSeven, dxSkinSharp,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinValentine, dxSkinXmas2008Blue, DB, ADODB, StdCtrls, ToolWin, ComCtrls,
  RzButton, ExtCtrls, RzPanel, ActnList;

type
  TQueryBase = class(TForm)
    gbHead: TcxGroupBox;
    gbMaster: TcxGroupBox;
    gbDetail: TcxGroupBox;
    dbgMaster: TDBGridEh;
    dbgDetail: TDBGridEh;
    RzToolbar: TRzToolbar;
    RzToolButton1: TRzToolButton;
    RzToolButton2: TRzToolButton;
    ActionList1: TActionList;
    AExcuteQRY: TAction;
    qryMaster: TADOQuery;
    qryDetail: TADOQuery;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure AExcuteQRYExecute(Sender: TObject);virtual;
  private
    { Private declarations }
    FDetailQueryID: string;
    procedure SetMasterQueryID(const Value:string);
    procedure SetDetailQueryID(const Value:string);
  public
    { Public declarations }
    function QueryInit:boolean;virtual;
  public
    property MasterQueryID : string write SetMasterQueryID;
    property DetailQueryID : string write SetDetailQueryID;
  end;


implementation

{$R *.dfm}

{ TQueryBase }

procedure TQueryBase.AExcuteQRYExecute(Sender: TObject);
begin
//
end;

procedure TQueryBase.FormCreate(Sender: TObject);
begin
  QueryInit;
end;

function TQueryBase.QueryInit: boolean;
begin
  qryMaster.Connection:= TADOConnection(SYSDB.GetDBConnection);
  qryDetail.Connection:= TADOConnection(SYSDB.GetDBConnection);
end;

procedure TQueryBase.SetDetailQueryID(const Value: string);
var Q:TADOQuery;
    fname,fchnname,fwidth:TStringList;
    I:integer;
    Col:TColumnEh;
begin
  FDetailQueryID:=Value;

  if Value='' then
  begin
    gbDetail.Visible:=False;
    exit;
  end;

  fname:=TStringList.Create;
  fchnname:=TStringList.Create;
  fwidth:=TStringList.Create;

  Q:=TADOQuery.Create(nil);
  Q.Connection:=TADOConnection(SYSDB.GetDBConnection);
  Q.SQL.Add('select * from DBGridSet where FormID=:ID and DBGridName=:Name');
  Q.Parameters.ParamByName('ID').Value:=Value;
  Q.Parameters.ParamByName('Name').Value:='DETAIL';
  Q.Open;
  if not Q.Eof then
  begin
    fname.CommaText:=Q.FieldByName('FieldList').AsString;
    fchnname.CommaText:=Q.FieldByName('FieldCHNName').AsString;
    fwidth.CommaText:=Q.FieldByName('FieldWidth').AsString;
  end;
  dbgDetail.Columns.Clear;
  for I := 0 to fname.Count - 1 do
  begin
    Col:=dbgDetail.Columns.Add;
    Col.FieldName:=fname[I];
    Col.Title.Caption:=fchnname[I];
    Col.Title.Alignment:=taCenter;
    Col.Width:=strtoint(fwidth[I]);
  end;

  FreeAndNil(fname);
  FreeAndNil(fchnname);
  FreeAndNil(fwidth);
  FreeAndNil(Q);
end;

procedure TQueryBase.SetMasterQueryID(const Value: string);
var Q:TADOQuery;
    fname,fchnname,fwidth:TStringList;
    I:integer;
    Col:TColumnEh;
begin
  fname:=TStringList.Create;
  fchnname:=TStringList.Create;
  fwidth:=TStringList.Create;

  Q:=TADOQuery.Create(nil);
  Q.Connection:=TADOConnection(SYSDB.GetDBConnection);
  Q.SQL.Add('select * from DBGridSet where FormID=:ID and DBGridName=:Name');
  Q.Parameters.ParamByName('ID').Value:=Value;
  Q.Parameters.ParamByName('Name').Value:='MASTER';
  Q.Open;
  if not Q.Eof then
  begin
    fname.CommaText:=Q.FieldByName('FieldList').AsString;
    fchnname.CommaText:=Q.FieldByName('FieldCHNName').AsString;
    fwidth.CommaText:=Q.FieldByName('FieldWidth').AsString;
  end;
  dbgMaster.Columns.Clear;
  for I := 0 to fname.Count - 1 do
  begin
    Col:=dbgMaster.Columns.Add;
    Col.FieldName:=fname[I];
    Col.Title.Caption:=fchnname[I];
    Col.Title.Alignment:=taCenter;
    Col.Width:=strtoint(fwidth[I]);
  end;

  FreeAndNil(fname);
  FreeAndNil(fchnname);
  FreeAndNil(fwidth);
  FreeAndNil(Q);
end;

end.
